<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>set</title>
</head>

<body>
  <input type="button" value="login" id="login">
  <script>
    //1.自定义迭代器初始化Set.
    const s1 = new Set({
      [Symbol.iterator]: function* () {
        yield "v1";
        yield "v2";
        yield "v3";
      }
    })
    console.log(s1.size);//3
    //2.CRUD
    const s = new Set().add("Matt").add("Sloan").add("hhh");
    console.log(s.has("hhh"), s.size); //true 3
    const objVal = {};
    const arrVal = [];
    s.add(objVal);
    s.add(arrVal);
    objVal.bar = "bar";
    arrVal.push("bar");
    console.log(s.has(objVal), s.has(arrVal), s.delete(objVal), s.delete(objVal)); //true true true false
    //迭代 s.values() === s[Symbol.iterator]();
    for (let value of s.values()) {
      console.log(value);
    }
    for (let value of s[Symbol.iterator]()) {
      console.log(value);
    }
    //扩展运算符集合转数组
    console.log([...s]);
    //按照插入顺序产生包含两个元素的数组.
    for (let pari of s.entries()) {
      console.log(pari);
    }
    //WeakSet();值必须是对象形式的,原始值要包装成对象.
    const ws = new WeakSet();
    const v1 = { id: 1 }, v2 = { id: 2 }, v3 = { id: 3 };
    ws.add(v1).add(v2).add(v3);
    console.log(ws);
    //弱值
    const ws1 = new WeakSet();
    const container = {
      val: {}
    }
    ws1.add(container.val);
    //container集合维护着一个对弱集合值的引用,这个对象值不会被垃圾回收.

    //应用:给对象打标签
    const disabledEl = new WeakSet();
    const loginBtn = document.querySelector("#login");
    disabledEl.add(loginBtn);//打上禁用标签,任何元素被删除,垃圾回收程序都可以释放其内存.
    //...扩展运算符执行的是浅拷贝
    let arr1 = [1, 2, 3];
    let arr2 = [...arr1];
    console.log(arr1, arr2, arr1 === arr2);//false
  </script>
</body>

</html>